package tech.aistar.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tech.aistar.dao.IProductDao;
import tech.aistar.mapper.ProductMapper;
import tech.aistar.model.pojo.Product;
import tech.aistar.model.pojo.ProductType;
import tech.aistar.model.vo.ProductQueryVo;
import tech.aistar.service.IProductService;
import tech.aistar.service.IProductTypeService;

import java.util.List;

@Controller
public class ProductController {

    @Autowired
    private IProductDao iProductDao;

    @Autowired
    private IProductService iProductService;

    @Autowired
    private IProductTypeService iProductTypeService;


    @GetMapping("/product/index")
    public String index3(Model model){
        List<Product> productList = iProductDao.findAll();
        model.addAttribute(productList);
        return "index-3";
    }

    @GetMapping("/product/shop")
    public String product(Model model,String pageNow, String searchName, String typeId){
        if(null == pageNow || pageNow.trim().length()==0){
            pageNow = "1";
        }

        ProductQueryVo queryVo = new ProductQueryVo();

        if(typeId!=null){
            queryVo.setProductTypeId(Integer.valueOf(typeId));
            model.addAttribute("typeId",typeId);
        }


        if(searchName!=null && searchName.trim().length()>0){
            model.addAttribute("searchName",searchName);
            queryVo.setName(searchName);
        }

        PageInfo<Product> pageInfo = iProductService.findAll(queryVo,Integer.valueOf(pageNow),10);
        List<ProductType> productTypes = iProductTypeService.findAll();
        model.addAttribute("productTypes",productTypes);
        model.addAttribute("pageInfo",pageInfo);
        return "shop";
    }

    @GetMapping("/product/details")
    public String product(Model model,String pId){

        if(null != pId || pId.trim().length()>0){
            Product product = iProductService.getById(Integer.valueOf(pId));
            System.out.println(product);
            model.addAttribute("product",product);
        }

        return "product-details";
    }
}
